package com.hzgj.soa.spring.schema;

import com.hzgj.soa.spring.ServerBean;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by ye.pan on 2015/7/9.
 */
public class ServerBeanBeanDefinitionParser extends SoaNamespaceHandler.CommonBeanDefinitionParser {

    AtomicBoolean regServer = new AtomicBoolean(false);

    public ServerBeanBeanDefinitionParser(Class<?> beanClass, boolean required) {
        super(beanClass, required);
    }

    @Override
    void parse0(RootBeanDefinition beanDefinition, Element element, ParserContext parserContext, Class<?> beanClass) {
        super.parse0(beanDefinition, element, parserContext, beanClass);
        if (regServer.compareAndSet(false, true)) {
            RootBeanDefinition serverBeanDefinition = new RootBeanDefinition();
            serverBeanDefinition.setBeanClass(ServerBean.class);
            serverBeanDefinition.setLazyInit(false);
            parserContext.getRegistry().registerBeanDefinition(ServerBean.class.getSimpleName(), serverBeanDefinition);
        }
    }
}
